//
//  SettingVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/29.
//

import UIKit

class SettingVC: ZMViewController, UITableViewDelegate, UITableViewDataSource {
    //MARK: --------------------属性--------------------
    let padding : CGFloat = 15
    
    let data : [[(title : String, subTitle : String, icon : Bool, id : Int)]] = [
        [(title : "修改手机号", subTitle : ZMAppShareData.share.safeUserInfo.phone.zm.hidPhone() , icon : true, id : 0), (title : "修改密码", subTitle : "", icon : true, id : 1)],
        [(title : "交易设置", subTitle : "", icon : true, id : 2)],
        [(title : "原有账户", subTitle : "", icon : true, id : 7),
         (title : "用户协议", subTitle : "", icon : true, id : 3),
         (title : "关于我们", subTitle : "", icon : true, id : 4),
         (title : "注销账号", subTitle : "", icon : true, id : 6),
         (title : "当前版本", subTitle : "v\(AppVersion)", icon : false, id : 5)]
    ]
    //MARK: --------------------UI控件--------------------
    let tableView = UITableView(frame: CGRect.zero, style: .plain).then { t in
        t.separatorStyle = .none
        t.estimatedSectionFooterHeight = 0.01
        t.estimatedSectionHeaderHeight = 0.01
        t.sectionFooterHeight = 0.01
        t.sectionHeaderHeight = 0.01
    }
    
    let button = ZMUtil.button(title: "退出登录", font: UIFont.systemFont(ofSize: 16, weight: .medium), color: "退出登录".zm.hex())

    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    
    //MARK: --------------------UI--------------------
    func setUI() {
        title = "系统设置"
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        
        let v = UIView()
        tableView.tableFooterView = v
        tableView.backgroundColor = "#F5F5F5".zm.hex()
        view.backgroundColor = "#F5F5F5".zm.hex()
        
        v.frame = CGRect(x: 0, y: 0, width: SCREEN_W, height: 120)
        v.backgroundColor = .clear
        
        v.addSubview(button)
        button.backgroundColor = .white
        button.layer.cornerRadius = 23
        button.snp.makeConstraints { make in
            make.height.equalTo(46)
            make.top.equalToSuperview().offset(48)
            make.right.left.equalToSuperview()
        }
        tableView.zm.register(nibName: "SettingCell", reuserId: "SettingCell")
        
        // nav right
//        setRightCustomBtn(titleStr: "其他", color: "#666666".zm.hex(), font: UIFont.systemFont(ofSize: 14))
    }
    
    func addConstraints() {
        tableView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(padding)
            make.right.equalToSuperview().offset(-padding)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        button.rx.tap.subscribe(onNext:{ [unowned self] resp in
            // 退出登录
            ZMAlertView.show(title: "提示", content: "是否要退出登录", left: "取消", right: "确认") {
                
            } onRight: {
                ZMJumpHelper.destroy(FromVC: self, toFirstClass: MineVC.classForCoder(), isContainSelf: true)
                AppHelper.currentNav()?.tabBarController?.selectedIndex = 0
                LoginService.shared.loginOut(false)
            }

        }).disposed(by: disposeBag)
    }

    //MARK: --------------------delegate--------------------
    func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[section].count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 16
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 48
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let v = UIView()
        v.backgroundColor = .red
        return v
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SettingCell") as! SettingCell
        let data = data[indexPath.section][indexPath.row]
        cell.titleLabel.text = data.title
        cell.subLabel.text = data.subTitle
        cell.rightIcon.isHidden = !data.icon
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let allCount = self.data[indexPath.section].count
        let current = indexPath.row
        if allCount == 1 {
            ZMUtil.clipCircle(view: cell, Corner: [.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 12)
        } else {
            if current == 0 {
                ZMUtil.clipCircle(view: cell, Corner: [.topLeft, .topRight,], radius: 12)
            } else if current == allCount - 1 {
                ZMUtil.clipCircle(view: cell, Corner: [.bottomLeft, .bottomRight], radius: 12)
            } else {
                cell.layer.mask = nil
            }
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = data[indexPath.section][indexPath.row]
        if item.id == 0 {
            router.push("xm://main/Mine/set/change_phone_one", context: ["phone":ZMAppShareData.share.safeUserInfo.phone])
        } else if item.id == 1 {
            router.push("xm://main/Login/find_psd", context: ["phone":ZMAppShareData.share.safeUserInfo.phone, "type":1])
        } else if item.id == 2 {
            if ZMAppShareData.share.safeUserInfo.isTradeOk {
                router.push("xm://main/Mine/trade_set")
            } else {
                let vc = TradeFroget()
                self.navigationController?.pushViewController(vc, animated: true)
            }
        } else if item.id == 3 {
            CommonService.shared.goWeb(type: .userProtocol)
        } else if item.id == 4 {
            CommonService.shared.goWeb(type: .aboutUs)
        } else if item.id == 5 {
            
        } else if item.id == 6 {
            let vc = DeleteAccountVC()
            self.navigationController?.pushViewController(vc, animated: true)
        } else if item.id == 7 {
            let vc = OldAccount()
            self.navigationController?.pushViewController(vc, animated: true)
        } else {
            ZMToast.showToast(title: "敬请期待")
        }
    }
}
